package org.sun.sunshine.hp.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.sun.sunshine.hp.dao.HappyDao;
import org.sun.sunshine.hp.entity.Happy;
import org.sun.sunshine.hp.service.HappyService;

@Service
public class HappyServiceImpl extends ServiceImpl<HappyDao, Happy> implements HappyService {
    @Autowired
    HappyDao happyDao;

    @Transactional
    @DS("db1")
    public boolean saveIfNotExist(Happy happy)
    {
        if (!happyDao.selectListByName(happy.getName()).isEmpty()){
            return false;
        }
        return saveOrUpdate(happy);
    }

    public IPage<Happy> page(Happy happy){
        IPage<Happy> ipage=new Page<>(1,10);
        QueryWrapper<Happy> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(happy.getName()),"name",happy.getName());
        return happyDao.selectPage(ipage,queryWrapper);
    }

}
